package com.class05;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
@SuppressWarnings("all")
public class LogFilter implements Filter {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
//        System.out.println("LogFilter访问目标资源之前..........");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        System.out.println("请求的URL地址：" + request.getRequestURI());
        System.out.println("请求的参数：" + request.getParameter("username"));
        System.out.println("请求的方式：" + request.getMethod());
        int serverPort = request.getServerPort();
        System.out.println("请求的服务器端口号：" + serverPort);
        request.setAttribute("msg", "Hello");
        long l = System.currentTimeMillis();
        System.out.println("请求开始时间：" + sdf.format(l));

//        doFilter(); 放行
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("LogFilter访问目标资源之后..........");
        long l1 = System.currentTimeMillis();
        System.out.println("请求结束时间：" + sdf.format(l1));
        System.out.println("请求处理时长：" + (l1 - l) + "ms");



    }
}
